package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.Acl_user;
import com.test.ws.s1.bean.ClassIf;
import com.test.ws.s1.bean.Route;
import com.test.ws.s1.service.ClassIfService;
import com.test.ws.s1.service.RouteService;
import com.test.ws.s1.service.SupplierService;
import com.test.ws.s1.utils.Res;
import com.test.ws.s1.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;

@Controller
public class RouteController {
    @Autowired
    private RouteService routeService;

    @Autowired
    private ClassIfService classIfService;

    @Autowired
    private SupplierService supplierService;

    /// 查询
    @RequestMapping("/routeCha")
    public String selectRoute(Route route, HttpServletRequest request,
                              @RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "3") int pageSize) {
        /// 旅游线路
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        ArrayList<Route> routes = routeService.selectRoute(route);
        PageInfo pageInfo = new PageInfo(routes);

        /// 分类
        ArrayList<ClassIf> classIfs = classIfService.selectClassIf(new ClassIf());
        request.getSession().setAttribute("classIfs",classIfs);

        /// 供应商
        ArrayList<com.test.ws.s1.bean.Supplier> suppliers = supplierService.selectSupplier(new com.test.ws.s1.bean.Supplier());
        request.getSession().setAttribute("suppliers",suppliers);

        request.getSession().setAttribute("routeCha",routes);
        request.setAttribute("pageInfo",pageInfo);
        return "houtai/route";
    }

    /// 进入增加页面
    @RequestMapping("/routeAdd")
    public String routeAdd(HttpServletRequest request) {
        return "houtai/routeZj";
    }

    /// 增加
    @RequestMapping("/insertRoute")
    public String insertRoute(Route users, HttpServletRequest request, MultipartFile image) throws IOException {
        /// 1. image.getSize() > 0 检查上传的文件是否非空。
        if (image.getSize()>0){
            /// 2. 使用UUID.randomUUID()生成一个全局唯一标识符，并通过replaceAll("-", "")去掉其中的-符号。
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            /// 3. 提取文件后缀名：image.getOriginalFilename().split("\\.")[1]，假设文件名为example.jpg，则提取到后缀jpg。
            String houZhui = image.getOriginalFilename().split("\\.")[1];

            image.transferTo(new File(Res.fileDir + uuid+"."+houZhui));

            /// 4. 将UUID与原始文件名拼接起来，生成新的文件名，并调用users.setTx()将其保存到Users对象中。
            users.setRoute_img(uuid+"."+houZhui);

        }

        /// 5. 调用userSever.insertUsers()方法将Users对象插入到数据库中。
        int i = routeService.insertRoute(users);

        /// 6. 如果插入成功，重定向到/users/selectUsers，否则返回"insert"视图。
        if (i>0){
            return "redirect:/routeCha";
        }
        return "redirect:/routeCha";
    }

    /// 删除
    @RequestMapping("/deleteRouteWei")
    public String deleteRouteWei(Route route,HttpServletRequest request) {
        String id = request.getParameter("id");
        route.setRoute_id(id);
        System.out.println(id);
        int i = routeService.deleteRouteWei(route);
        System.out.println(i);
        if (i>0){
            return "redirect:/routeCha";
        }
        return "redirect:/routeCha";
    }

    /// 进入修改页面
    @RequestMapping("/selectRouteOne")
    public String selectRouteOne(Route route,HttpServletRequest request) {
        String id = request.getParameter("id");
        route.setRoute_id(id);
        Route routeOne = routeService.selectRouteOne(route);
        System.out.println("=================!!!!!!!!!!!!!!!!!!"+routeOne);
        request.getSession().setAttribute("routess",routeOne);
        return "houtai/routeXiuGai";
    }

    /// 修改
    @RequestMapping("/updateRoute")
    public String updateRoute(Route user,HttpServletRequest request,MultipartFile image) throws IOException {
        if (image != null && image.getSize() > 0) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            ///新增加的头像路径
            String path = Res.fileDir + uuid + "." + houZhui;
            image.transferTo(new File(path));
            user.setRoute_img(uuid + "." + houZhui);
            /// 获取旧头像
            Route route = routeService.selectRouteOne(user);
            String tx = route.getRoute_img();

            if (tx != null && !tx.equals(user.getRoute_img())) {
                File file = new File(Res.fileDir + tx);
                if (file.exists()) {
                    file.delete();
                }
            }

        }
        int i = routeService.updateRoute(user);
        if (i > 0) {
            return "redirect:/routeCha";
        }
        return "redirect:/routeCha";
    }



    /// 查询轮播图
    @RequestMapping("/routeLunBo")
    public String selectRouteLunBo(Route route, HttpServletRequest request) {
        ArrayList<Route> routes = routeService.selectRouteLunBo();
        request.setAttribute("routeLunBo", routes);
        return "index";
    }

    /// 修改轮播图
    @RequestMapping("/updateRouteLunBo")
    public String updateRouteLunBo(Route route, HttpServletRequest request) {
        String id = request.getParameter("id");
        route.setRoute_id(id);
        int i = routeService.updateRouteLunBo(route);
        if (i > 0) {
            return "redirect:/routeCha";
        }
        return "redirect:/routeCha";
    }

    /// 禁用轮播图
    @RequestMapping("/updateRouteLunBoWei")
    public String updateRouteLunBoWei(Route route, HttpServletRequest request) {
        String id = request.getParameter("id");
        route.setRoute_id(id);
        int i = routeService.updateRouteLunBoWei(route);
        if (i > 0) {
            return "redirect:/routeCha";
        }
        return "redirect:/routeCha";
    }

}
